/**
 * Filename: SettingActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 2:36
 */
package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

public class TimeCardFromActivity extends Activity implements View.OnClickListener {
    private EditText et_write_from;
    private TextView tv_write_complete;
    private TextView tv_write_cancel;
    private InputMethodManager imm;
    private String text_content = "";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // Translucent navigation bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setContentView(R.layout.yn_activity_timecard_from);
        initView();
    }


    private void initView() {
        et_write_from = (EditText) findViewById(R.id.et_write_from);
        tv_write_complete = (TextView) findViewById(R.id.tv_write_complete);
        tv_write_cancel = (TextView) findViewById(R.id.tv_write_cancel);
        findViewById(R.id.timecard_from_enty_scroll).setOnClickListener(this);
        tv_write_complete.setOnClickListener(this);
        tv_write_cancel.setOnClickListener(this);
        et_write_from.setFilters(new InputFilter[]{filter});
        text_content = getIntent().getStringExtra("text_content");
        et_write_from.setText(text_content);
        et_write_from.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (et_write_from.length() >= 20) {
                    ToastUtils.showToast(TimeCardFromActivity.this, "最多输入10个字", 1000);
                }
            }
        });
    }

    final int maxLen = 20;
    InputFilter filter = new InputFilter() {

        @Override
        public CharSequence filter(CharSequence src, int start, int end, Spanned dest, int dstart, int dend) {
            int dindex = 0;
            int count = 0;

            while (count <= maxLen && dindex < dest.length()) {
                char c = dest.charAt(dindex++);
                if (c < 128) {
                    count = count + 1;
                } else {
                    count = count + 2;
                }
            }

            if (count > maxLen) {
                return dest.subSequence(0, dindex - 1);
            }

            int sindex = 0;
            while (count <= maxLen && sindex < src.length()) {
                char c = src.charAt(sindex++);
                if (c < 128) {
                    count = count + 1;
                } else {
                    count = count + 2;
                }
            }

            if (count > maxLen) {
                sindex--;
            }

            return src.subSequence(0, sindex);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_write_complete:
                Intent mIntent = new Intent(ACTIONs.actionUpdateFromText);
                mIntent.putExtra("tx", et_write_from.getText().toString());
                sendBroadcast(mIntent);
                imm.hideSoftInputFromWindow(et_write_from.getWindowToken(), 0);
                finish();
                break;
            case R.id.tv_write_cancel:
                imm.hideSoftInputFromWindow(et_write_from.getWindowToken(), 0);
                finish();
                break;
            case R.id.timecard_from_enty_scroll:
                imm.hideSoftInputFromWindow(et_write_from.getWindowToken(), 0);
                finish();
                break;

        }
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
